using System;
using Microsoft.SPOT;
using Microsoft.SPOT.Presentation.Controls;
using Microsoft.SPOT.Presentation.Media;
using Microsoft.SPOT.Presentation;

namespace MicroSnake
{
    class ListBoxImageTextMenuItem : ListBoxItem
    {
        private Text text;
        private Image image;
        private StackPanel stackPanel;

        public ListBoxImageTextMenuItem(Image image, Text text)
            : base()
        {
            this.text = text;
            this.image = image;

            stackPanel = new StackPanel(Orientation.Horizontal);
            stackPanel.HorizontalAlignment = HorizontalAlignment.Center;
            stackPanel.Children.Add(image);
            stackPanel.Children.Add(text);

            this.Child = stackPanel;
            this.Background = null;            
        }

        public override string ToString()
        {
            return text.TextContent;
        }

        public override void OnRender(DrawingContext dc)
        {
            base.OnRender(dc);
        }

        protected override void OnIsSelectedChanged(bool isSelected)
        {
            if (isSelected)
            {
                this.Background = new SolidColorBrush(MicroSnakeColors.MenuItemSelectedBackgroundColor);
                text.ForeColor = MicroSnakeColors.MenuItemSelectedForeColor;
            }
            else
            {
                this.Background = null;  // Trasparent
                text.ForeColor = MicroSnakeColors.MenuItemForeColor;
            }
        }

        /// <summary>
        /// Build a list box menu item with a specific content
        /// </summary>
        /// <param name="stringResource"></param>
        /// <returns></returns>
        public static ListBoxImageTextMenuItem Make(Resources.BitmapResources bitmapResource, Resources.StringResources stringResource)
        {
            Image image = new Image(Resources.GetBitmap(bitmapResource));
            image.HorizontalAlignment = HorizontalAlignment.Center;
            image.VerticalAlignment = VerticalAlignment.Center;
            image.SetMargin(6);

            Text txtItem = new Text();
            txtItem.Font = Resources.GetFont(Resources.FontResources.small);
            txtItem.ForeColor = MicroSnakeColors.MenuItemForeColor;
            txtItem.HorizontalAlignment = HorizontalAlignment.Left;
            txtItem.TextAlignment = TextAlignment.Left;
            txtItem.VerticalAlignment = VerticalAlignment.Center;
            txtItem.SetMargin(6);

            txtItem.TextContent = Resources.GetString(stringResource);
            
            return new ListBoxImageTextMenuItem(image, txtItem);
        }
    }
}
